package com.erp.warehouse.clouderp_warehouse.controller;

import com.erp.warehouse.clouderp_warehouse.pojo.cost.CostAdjustmentCreate;
import com.erp.warehouse.clouderp_warehouse.pojo.cost.CostAdjustmentModify;
import com.erp.warehouse.clouderp_warehouse.service.cost.CostAdjustService;
import com.erp.warehouse.clouderp_warehouse.util.UniqueOrderCode;
import org.comple.erp.pojo.ResultStatus;
import org.comple.erp.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 2025/11/12 10:23
 * author: 向远洋
 */
@RestController
@RequestMapping("/costAdjustment")
public class CostAdjustmentController {
    @Autowired
    CostAdjustService costAdjustService;
    @Autowired
    UniqueOrderCode uniqueOrderCode;
    @Value("${auth.staffId}")
    String staffId;
    @Value("${auth.companyId}")
    String companyId;

    @PostMapping("/page")
    public AjaxResult getPage(@RequestBody Map<String, Object> params) {
        params.put("companyId", companyId);
        return AjaxResult.back(ResultStatus.SUCCESS, "获取成本调整单成功")
                .add("pageInfo", costAdjustService.getPageInfo(params));
    }

    @PostMapping("/create")
    public AjaxResult createCostAdjustmentWithItem(@RequestBody CostAdjustmentCreate request) throws InterruptedException {
        //从jwt获取到用户，和相关公司code + staffName等
        String staffNo = "S1001", companyCode = "CNY001";
        //获取到当前订单编号
        String code = "CB" + uniqueOrderCode.getCode(companyCode, "COST_ADJUST");
        request.getCostAdjustment().setCode(code);
        request.getCostAdjustment().setStaffId(staffId);
        request.getCostAdjustment().setStaffName("xxx员工");
        request.getCostAdjustment().setCompanyId(companyId);
        costAdjustService.createWithItems(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "成本调整单创建成功");
    }

    @PostMapping("/get/{id}")
    public AjaxResult getCostAdjustmentWithDetail(@PathVariable String id) {
        return AjaxResult.back(ResultStatus.SUCCESS, "获取成本调整单和成本调整明细成功！")
                .add("costAdjustRequest", costAdjustService.getCostAdjustmentWithDetail(id));
    }

    @PostMapping("/modify")
    public AjaxResult modifyCostAdjustmentWithDetail(@RequestBody CostAdjustmentModify request) {
        costAdjustService.modifyCostAdjustmentWithDetail(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "修改成本调整单和成本调整明细成功！");
    }

    @PostMapping("/auditor")
    public AjaxResult auditorCostAdjustmentWithDetail(@RequestBody CostAdjustmentCreate request) {
        costAdjustService.auditorCostAdjustmentWithDetail(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "审核成本调整单和成本调整明细成功");
    }
}
